package com.example.anquan.controller;

import com.example.anquan.Service.ArticleService;
import com.example.anquan.Service.CommentService;
import com.example.anquan.Service.UserService;
import com.example.anquan.guanli.Article;
import com.example.anquan.guanli.Comment;
import com.example.anquan.guanli.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping(value="/back")
public class BackController {
    @Autowired
    private ArticleService articleService;

    @Autowired
    private CommentService commentService;

    @Autowired
    private UserService userService;
    //文章管理页面
    @GetMapping("/article")
    public String articleManager(Model model) {
        List<Article> articles = articleService.findAll();
        model.addAttribute("articles",articles);
        return "/back/article";
    }
    //文章删除
    @GetMapping("/article/delete/{id}")
    public String deleteArticle(@PathVariable("id") Integer id){
        articleService.deleteById(id);
        return "redirect:/back/article";
    }
    //文章修改页面
    @GetMapping("/edit/{id}")
    public String editArticlePage(@PathVariable("id") Integer id, Model model){
        model.addAttribute("article", articleService.findArticleById(id));
        return "/back/edit";
    }
    //文章新增页面
    @GetMapping("/article/add")
    public String addArticlePage(){
        return "/back/edit";
    }
    //文章修改发布
    @PostMapping("/article/publish/{id}")
    public String editArticle(@PathVariable("id") Integer id,
                              @RequestParam("title") String title, @RequestParam("category") String category,
                              @RequestParam("content") String content){
        articleService.updateArticleById(id, title, category, content);
        return "redirect:/article/"+id;
    }
    //文章新增发布
    @PostMapping("/article/publish")
    public String addArticle(@RequestParam("title") String title,
                             @RequestParam("category") String category, @RequestParam("content") String
                                     content){
        Article article = articleService.addArticle(title, category, content);
        return "redirect:/article/"+article.getId();
    }
    //评论管理
    @GetMapping("/comment")
    public String commentManager(Model model) {
        List<Comment> comments = commentService.findAll();
        model.addAttribute("comments", comments);
        return "/back/comment";
    }

    //用户管理
    @GetMapping("/user")
    public String UserManager(Model model) {
        List<User> User = userService.findAll();
        model.addAttribute("users", User);
        return "/back/User";
    }
    //id删除
    @GetMapping("/comment/delete/{id}")
    public String deletecomment(@PathVariable("id") Integer id, Model model) {
        commentService.deleteDiscuss(id);
        return "redirect:/back/comment";
    }
}
